const webpack = require('webpack');
const helper = require('./helper');
const HappyPack = require('happypack');
const happyThreadPool = HappyPack.ThreadPool({ size: 2 });
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');

if (!process.env.ENV) process.env.ENV = 'dev';
var env = process.env.ENV.toUpperCase();
console.log('Current enviroment is: ' + env);

module.exports = function (options) {
    isProd = options && options.env === 'production';
    outPutDir = options && options.outPutDir || 'dist';
    let config =
        {
            module: {
                loaders: [{
                    test: /.ts$/,
                    loaders: ['happypack/loader?id=ts'],
                    exclude: /node_modules/,
                    include: helper.root('src')
                },
                {
                    test: /\.html$/,
                    loader: 'happypack/loader?id=html',
                    include: helper.root('src'),
                    exclude: helper.root('src/index.html')
                },
                {
                    test: /\.scss$/,
                    loader: 'happypack/loader?id=scss',
                    include: helper.root('src'),
                }, {
                    test: /\.css$/,
                    loaders: ['happypack/loader?id=css'],
                    include: helper.root('src'),
                    exclude: helper.root('styles.css')
                },
                {
                    test: /\.css$/,
                    loaders: ['raw-loader'],
                    include: helper.root('styles.css'),
                },
                {
                    test: /\.(jpg|png|gif)|(eot|woff2?|svg|ttf)([\?]?.*)$/,
                    use: 'file-loader'
                }
                ]
            },

            plugins: [
                new HappyPack({
                    id: 'ts',
                    threadPool: happyThreadPool,
                    //cache: true,
                    loaders: [{
                        path: 'ts-loader',
                        query: { transpileOnly: true, happyPackMode: true }
                    }, {
                        path: 'angular2-template-loader',
                        query: { happyPackMode: true }
                    }, {
                        path: 'angular2-router-loader',
                        query: { happyPackMode: true }
                    }],

                    verbose: true
                }),

                new HappyPack({
                    id: 'scss',
                    //cache: true,
                    threadPool: happyThreadPool,
                    loaders: ['to-string-loader', 'css-loader', 'sass-loader']
                }),

                new HappyPack({
                    id: 'css',
                    //cache: true,
                    threadPool: happyThreadPool,
                    loaders: ['to-string-loader', 'style-loader', 'css-loader']
                }),

                new HappyPack({
                    id: 'html',
                    //cache: true,
                    threadPool: happyThreadPool,
                    loaders: ['raw-loader']
                }),

                new ForkTsCheckerWebpackPlugin(),

                new ContextReplacementPlugin(
                    /angular(\\|\/)core(\\|\/)@angular/,
                    helper.root('src'), // location of your src
                    {

                    }
                ),
            ],
            resolve: {
                extensions: ['.js', '.ts', '.json'],
                modules: [helper.root('src'), helper.root('node_modules')],
            }
        };

    return config;
}
